package com.prs.crm.action.document;

import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.document.Theme;
import com.prs.crm.domain.document.ThemeType;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.document.ThemeService;
import com.prs.crm.service.document.ThemeTypeService;

public class ThemeAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Theme theme;
	private ThemeService themeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private List<ThemeType> themeType;
	private ThemeTypeService themeTypeService;
	private TreeNode root;
	public TreeNode getRoot() {
		return root;
	}
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	public Theme getTheme() {
		return theme;
	}
	public void setTheme(Theme theme) {
		this.theme = theme;
	}
	public ThemeService getThemeService() {
		return themeService;
	}
	public void setThemeService(ThemeService themeService) {
		this.themeService = themeService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public List<ThemeType> getThemeType() {
		return themeType;
	}
	public void setThemeType(List<ThemeType> themeType) {
		this.themeType = themeType;
	}
	public ThemeTypeService getThemeTypeService() {
		return themeTypeService;
	}
	public void setThemeTypeService(ThemeTypeService themeTypeService) {
		this.themeTypeService = themeTypeService;
	}
	
	@Action("listTheme")
	public String listTheme(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Theme.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getThemeService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editTheme")
	public String editTheme(){
		this.themeType=this.getThemeTypeService().getAllThemeType();
		return SUCCESS;
	}
	@Action(value="saveTheme",results={
			@Result(name="success",type="redirect",location="listTheme"),
			@Result(name="error",type="chain",location="editTheme")
	})
	public String saveTheme(){
		this.getThemeService().save(theme);
		return SUCCESS;
	}
	
	@Action(value="saveAndTheme",results={
			@Result(name="success",type="redirect",location="editTheme"),
			@Result(name="error",type="chain",location="editTheme")
	})
	public String saveAndTheme(){
		this.getThemeService().save(theme);
		return SUCCESS;
	}
	@Action(value="removeTheme",results={
			@Result(name="success",type="redirect",location="listTheme"),
			@Result(name="error",type="chain",location="listTheme ")
	})
	public String removeTheme(){
		if (ids==null) {
			this.setPrompt("请选择一行！");
			return ERROR;
		}
		this.getThemeService().batchRemove(ids);
		return SUCCESS;
	}
	

	@Action(value = "loadThemeTree", results = { @Result(name = "success", type = "json", params = {
			"root", "root" }) })
	public String loadTree() {
		root = new TreeNode();
		root.setId("-1");

		TreeNode firstNode = new TreeNode();
		firstNode.setId("0");
		firstNode.setText("主题词选择");
		Collection<ThemeType> themeTypes=this.getThemeTypeService().getRootThemeType();
		for (ThemeType themeType : themeTypes) {
			TreeNode node = new TreeNode();
			node.setText(themeType.getName());
			node.setId("themeType-" + themeType.getId().toString());
			firstNode.addChildNode(node);
			addTheemeNode(node, themeType);
//			addThemeTypeNode(node, themeType);
		}
		root.addChildNode(firstNode);
		return SUCCESS;
	}

//	private void addThemeTypeNode(TreeNode parent, ThemeType themeType) {
//		for (ThemeType depart : themeType.getChilds()) {
//			TreeNode node = new TreeNode();
//			node.setText(depart.getName());
//			node.setId("themeType-" + depart.getId().toString());
//			parent.addChildNode(node);
//			addThemeTypeNode(node, depart);
//			addTheemeNode(node, depart);
//		}
//	}
	
	private void addTheemeNode(TreeNode parent, ThemeType themeType) {
		for (Theme theme: this.getThemeService().getRootTheme(themeType)) {
			TreeNode node = new TreeNode();
			node.setText(theme.getName());
			node.setId(theme.getId().toString());
			parent.addChildNode(node);
		}
	}
}
